<?php
function pass_protect($name, $pass, $mensaje='Area solo para usuarios registrados'){
	$files['.htaccess'] = array(
		'AuthName "'.$mensaje.'" ',
		'AuthType Basic ',
		'AuthUserFile '.realpath('./').'/.htpasswd ',
		//'AuthGroupFile /dev/null ',
		'require valid-user '
	);
	if(is_array($name) && is_array($pass)){
		foreach($name as $k => $v){
			if( !empty($name[$k]) && !empty($pass[$k]) ){   $files['.htpasswd'][] = $name[$k].':'.crypt($pass[$k]);	}
		}
	} elseif(is_array($name)){  foreach($name as $k => $v){   $files['.htpasswd'][] = $v.':'.crypt($pass);   }  
	} elseif(is_array($pass)){  foreach($pass as $k => $v){   $files['.htpasswd'][] = $name.':'.crypt($v);   }  
	} else {				$files['.htpasswd'][] = $name.':'.crypt($pass); 
	}
	foreach($files as $filename => $cont){
		$cont   = implode("\n", $cont);
		$fp  = fopen($filename, 'w+');
		$read   = fwrite($fp, $cont, strlen($cont));
		fclose($fp);
	}
	return true;
}
?>